package com.wzw.basics.collection.list;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * ArrayList 重要原理
 * @author Wangzhiwen
 */
public class ArrayListPrinciple {

    public static void main(String[] args) {
        dynamicExpansion();
    }

    /**
     * 动态扩容原理 grow
     */
    public static void dynamicExpansion(){

        // 无参构造，默认容量为10
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(1, 1);
        Iterator<Integer> iterator = arrayList.iterator();
        while (iterator.hasNext()) {
            iterator.next();
            iterator.remove();
        }
        // 添加元素时动态扩容
        arrayList.add(1);
        List<Integer> list = Collections.synchronizedList(arrayList);

        System.out.println(Arrays.toString(arrayList.toArray()));

        LinkedList<Integer> linkedList = new LinkedList<>();
        linkedList.add(1);
        Iterator<Integer> iterator1 = linkedList.iterator();
        linkedList.get(1);

        CopyOnWriteArrayList<Integer> copyOnWriteArrayList = new CopyOnWriteArrayList<>();
        copyOnWriteArrayList.add(1);
    }

}
